<?php
namespace app\api\service;
use think\Controller;
use think\Db;

class Comment extends Controller {
    static protected $err_code = 0;
    // 发表评论接口
    static public function publish_comment($param) {
        if(!isset($param['member_id']) ||
            !isset($param['obj_id']) ||
            !isset($param['type']) ||
            !isset($param['content']) ||
            !isset($param['level'])) return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];

        // 启动事务
        Db::startTrans();
        try{
            // 根据类型和对象(订单)ID获取商家ID
            $type = $param['type'];
            $obj_id = $param['obj_id'];
            $models = ['', 'order_buy', 'order_server','','','order_server_deal'];
            $fields = ['', 'shop_id', 'repair_id','','','repair_id'];
            $model = $models[$type];
            $field = $fields[$type];
            $merchant_id = db($model)->where('id', $obj_id)->value($field);

            $data = [
                'member_id' => $param['member_id'],
                'obj_id' => $obj_id,
                'type' => $type,
                'content' => $param['content'],
                'level' => $param['level'],
                'merchant_id' => $merchant_id,
                'add_time' => time()
            ];


            // 检查是否已评价
            $comment = Db::table('mob_comment')
                ->where('member_id', $param['member_id'])
                ->where('obj_id', $param['obj_id'])
                ->where('type', $param['type'])
                ->find();
            if ($comment) {
                self::$err_code = 400094;
                throw new \Exception('FAULT');
            }

            // 添加评论记录
            $comment_id = Db::table('mob_comment')->insertGetId($data); // 添加评论记录

            // 批量插入评论 label
            $data = [];
            if (isset($param['label'])) {
                $labels = explode(',', $param['label']);
                foreach ($labels as $v) {
                    if ($v) {
                        $data[] = [
                            'comment_id' => $comment_id,
                            'label' => $v
                        ];
                    }
                }
                Db::table('mob_comment_order_label')->insertAll($data);
            }

            $models = ['', 'order_buy', 'order_server','','','order_server_deal'];
            $model = $models[$param['type']];
            if($param['type']!=5){
                Db::table('mob_' . $model)->where('id', $param['obj_id'])->where('member_id', $param['member_id'])->update(['is_comment' => 1]); // 修改订单评论状态
            }else{
                Db::table('mob_order_server_deal')->where('id', $param['obj_id'])->where('member_id_to', $param['member_id'])->update(['is_comment' => 1]); // 修改订单评论状态
            }

            Db::commit();
            return [ 'code'=>200, 'msg'=> 'SUCCESS'];

        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return ['code'=>self::$err_code,'msg'=> $e->getMessage()];
        }
    }


    // 获取评论列表接口
    static public function get_comment_list($param) {
        if(!isset($param['obj_id']) || !isset($param['type'])) return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];
        try{
            $where['obj_id'] = $param['obj_id'];
            $where['type'] = $param['type'];
            $comment = Db::table('mob_comment')->where($where)->paginate();
            return ['data'=>$comment, 'code'=>200, 'msg'=> 'SUCCESS'];
        } catch (\Exception $e) {
            return ['code'=>self::$err_code,'msg'=> $e->getMessage()];
        }
    }

    // 获取用户评论接口
    static public function get_member_comment($param) {
        if(!isset($param['member_id'])) return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];
        try{
            $where['member_id'] = $param['member_id'];
            $comment = Db::table('mob_comment')->where($where)->paginate(20);
            return ['data'=>$comment, 'code'=>200, 'msg'=> 'SUCCESS'];
        } catch (\Exception $e) {
            return ['code'=>self::$err_code,'msg'=> $e->getMessage()];
        }

    }

}
